package com.apes.autoparts.supplier.platform;

import com.alibaba.fastjson.JSONObject;
import com.alibaba.fastjson.annotation.JSONField;
import com.apes.framework.jpa.BaseModel;
import com.apes.framework.jpa.DefaultPersistListener;
import com.apes.framework.jpa.comment.Comment;
import com.apes.framework.jpa.generator.ApesGenerator;
import com.apes.framework.plugin.soe.annotations.State;
import com.apes.framework.plugin.soe.annotations.States;
import com.apes.framework.util.DateUtil;
import com.apestech.framework.util.StringUtil;
import lombok.Getter;
import lombok.Setter;
import org.hibernate.annotations.DynamicInsert;
import org.hibernate.annotations.DynamicUpdate;
import org.hibernate.annotations.SelectBeforeUpdate;
import org.springframework.util.StringUtils;

import javax.persistence.*;
import java.util.*;

@Getter
@Setter
@SelectBeforeUpdate
@DynamicInsert
@DynamicUpdate
@Entity
@EntityListeners(DefaultPersistListener.class)
@Table(name = SupplierPlatformItem.CONST_TABLE_NAME, indexes = {@Index(columnList = "supplier_platform_id, access_key", unique = true)})
public class SupplierPlatformItem extends BaseModel {

    public static final String CONST_TABLE_NAME = "supplier_platform_item";

    @Id
    @Comment("流水码")
    @Column(name = "id", length = 32)
    @ApesGenerator(code = CONST_TABLE_NAME, name = "平台供应商")
    private String id;

    @ManyToOne(optional = false)
    @JoinColumn(name = "SUPPLIER_PLATFORM_ID", referencedColumnName = "id")
    @Comment("平台")
    private SupplierPlatform supplierPlatform;

    @Column(name = "supplier", length = 4000)
    @Comment("供应商")
    private String supplier; //供应商

    @Comment("接入key")
    @Column(name = "ACCESS_KEY", length = 60)
    private String accessKey;

    @States(name = "状态", values = {
            @State(id = "debugging" , name = "调试中"),
            @State(id = "normal", name = "正常")
    })
    @Column(name = "state", length = 30)
    private String state = "debugging";

    @Comment("供应商获取增量数据时间戳")
    @JSONField(format = DateUtil.SHOW_DATETIME_FORMAT)
    @Temporal(TemporalType.TIMESTAMP)
    @Column(name = "syn_increment_time")
    private Date synIncrementTime;

    @Transient
    private Map supplierMap;


    public Map getSupplierMap() {
        if (isWrite() || isLoad()) {
            supplierMap = StringToConvert(this.supplier);
        }
        return supplierMap;
    }
    public void setSupplierMap(Map map) {
        this.supplierMap = map;
        if (!isWrite()) {
            this.supplier = convertToString(this.supplierMap);
        }
    }


    public String convertToString(Map map) {
        StringBuffer sb = new StringBuffer();
        Set set = map.keySet();
        for (Object key : set) {
            sb.append(key.toString());
            sb.append(",");
            sb.append(map.get(key.toString()));
            sb.append(";");
        }
        return sb.toString();
    }

    @Transient
    public Map StringToConvert(String st) {
        Map map = new HashMap();
        if (StringUtil.isEmpty(st)) return null;
        String[] rows = st.split(";");
        for (String row : rows) {
            if (row.contains(",")) {
                map.put(StringUtils.split(row, ",")[0], StringUtils.split(row, ",")[1]);
            }
        }
        return map;
    }

}
